

// 全局定义一次
layui.config({
    base: window.location.protocol+"//"+window.location.host + '/public/static/'
}).extend({
    formSelects: 'plugins/layui-formSelects/formSelects-v4', // 加载 formSelects
    echarts: 'plugins/echarts/echarts', // 加载 echarts
    echartsTheme: 'plugins/echarts/echartsTheme',
});

layui.use(['element', 'layer', 'form', 'jquery'], function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;


    if($('#navBar').find('li').length <= 0){
        // 隐藏左侧导航
        $(".layui-layout-admin").toggleClass("showMenu");
    }

    // 隐藏左侧导航
    $(".hideMenu").click(function(){
        if($(".topLevelMenus li.layui-this a").data("url")){
            // 主要为了避免左侧显示的内容与顶部菜单不匹配
            layer.msg("此栏目状态下左侧菜单不可展开");
            return false;
        }
        $(".layui-layout-admin").toggleClass("showMenu");
    });

    // 关闭 iframe
    $(document).on('click', '.close-btn', function(){
        //parent.layer.close(layer.index);
        var indexLay = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
        parent.layer.close(indexLay);
    });

    // 个人中心
    $(document).on('click', '#view-profile', function(){
        var content = $(this).attr('data-url');
        var index = layer.open({
            title : "个人中心",
            type : 2,
            area : ["600px", "400px"],
            content : content,
            success : function(layero, index){
                form.render();
            }
        });
    });

});

function checkRate(value)
{
    var re = /^[1-9]+[0-9]*]*$/;
    if (re.test(value))
    {
        return true;
    }
    return false;
}

function formatMoney(s, type) {
    if (/[^0-9\.]/.test(s))
        return "0.00";
    if (s == null || s == "null" || s == "")
        return "0.00";
    s = s.toString().replace(/^(\d*)$/, "$1.");
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s))
        s = s.replace(re, "$1,$2");
    s = s.replace(/,(\d\d)$/, ".$1");
    if (type == 0) {
        var a = s.split(".");
        if (a[1] == "00") {
            s = a[0];
        }
    }
    return s;
}

function checkMoney(value) {
    if(value.trim() == ''){
        return false;
    }
    if(value <= 0){
        return false
    }
    if(!/^[0-9]+(.[0-9]{1,2})?$/.test(value)){
        return false
    }
    return true
}

/**
 * 产品单位
 * @return array
 */
function get_product_unit()
{
    var arr = [];
    arr[1] = '斤';
    arr[2] = '公斤';
    arr[3] = '两';
    arr[4] = 'g';
    arr[5] = 'kg';
    arr[6] = '只';
    arr[7] = '个';
    arr[8] = '片';
    arr[9] = '枚';
    arr[10] = '张';
    arr[11] = '粒';
    arr[12] = '瓶';
    arr[13] = '箱';
    arr[14] = '盒';
    arr[15] = '包';
    arr[16] = '罐';
    arr[17] = '袋';
    arr[18] = '坛';
    arr[19] = '桶';
    arr[20] = '升';
    arr[21] = '毫升';
    arr[22] = 'cm';
    arr[23] = 'mm';
    arr[24] = '把';
    arr[25] = '支';
    arr[26] = '听';
    arr[27] = '排';
    return arr;
}

// 显示产品单位
function show_product_unit(id)
{
    var units = get_product_unit();

    return units[id];
}

/**
 * 数组 去重
 */
function arr_unique (arr) {
    var res = [];
    var json = {};
    for(var i = 0; i < arr.length; i++){
        if(!json[arr[i]]){
            res.push(arr[i]);
            json[arr[i]] = 1;
        }
    }
    return res;
}